package com.shengqi.xinnongren.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.utils.CountDownTimerUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.dialog.VerifyCaptchaDialog;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.home.activities.MainActivity;
import com.shengqi.xinnongren.work.ui.user.model.CaptchaBean;
import com.shengqi.xinnongren.work.ui.user.presenter.BindPhonePresenter;
import com.shengqi.xinnongren.work.ui.user.view.BindPhoneView;

import butterknife.BindView;
import butterknife.OnClick;

public class BindPhoneActivity extends BaseMvpActivity<BindPhoneView, BindPhonePresenter> implements BindPhoneView {

    private static final String EXTRA_BUNDLE = "bundle";
    private static final String EXTRA_TYPE = "type";
    public static final int TYPE_WECHAT = 0x123;
    public static final int TYPE_ALI = 0x124;
    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.edt_captcha)
    EditText edt_captcha;
    @BindView(R.id.tv_captcha)
    TextView tv_captcha;

    private VerifyCaptchaDialog dialog;


    private int mType;

    /**
     * 微信登录所需参数
     */
    private String mUid;
    private String mName;
    private String mHeadUrl;
    /**
     * 支付宝登录所需参数
     */
    private String mUser_id;
    private String mAuth_code;


    public static void start(Context context, Bundle bundle, int type) {
        Intent intent = new Intent(context, BindPhoneActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        intent.putExtra(EXTRA_BUNDLE, bundle);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void initView() {

        Intent intent = getIntent();
        if (intent != null) {
            Bundle extras = intent.getBundleExtra(EXTRA_BUNDLE);
            mType = intent.getIntExtra(EXTRA_TYPE, 0);
            if (mType == TYPE_WECHAT) {
                mUid = extras.getString("uid");
                mName = extras.getString("name");
                mHeadUrl = extras.getString("headUrl");
            } else if (mType == TYPE_ALI) {
                mUser_id = extras.getString("user_id");
                mAuth_code = extras.getString("auth_code");
            }

        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected BindPhonePresenter createPresenter() {
        return new BindPhonePresenter();
    }

    @OnClick(R.id.tv_captcha)
    void showCaptchaDialog() {
        String strPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(strPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }
        mPresenter.getCaptchaUrl(strPhone);
    }


    @OnClick(R.id.btn_next)
    void btnNextClick() {
        String strPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(strPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }

        String strCaptcha = edt_captcha.getText().toString();
        if (TextUtils.isEmpty(strCaptcha)) {
            ToastHelper.getInstance(this).showWarn("请输入验证码");
            return;
        }
        mPresenter.checkCaptcha(strPhone, strCaptcha);

    }


    @Override
    public void showCaptchaUrl(HttpRespond<CaptchaBean> respond) {
        if (respond.result == 1) {
            CaptchaBean bean = respond.data;
            LogUtil.e(bean.captchaUrl);
            dialog = VerifyCaptchaDialog.newInstance(
                    edt_phone.getText().toString(), bean.captchaUrl, captcha -> mPresenter.sendSMS(edt_phone.getText().toString(), captcha));
            dialog.show(getSupportFragmentManager(), "captcha");

        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void sendSMSRes(HttpRespond respond) {
        if (respond.result == 1) {
            ToastHelper.getInstance(this).showSucceed("短信发送成功");
            if (dialog != null) {
                dialog.dismiss();
            }
            CountDownTimerUtils countDownTimerUtils =
                    new CountDownTimerUtils(tv_captcha, 60000, 1000);
            countDownTimerUtils.start();
        } else {
            ToastHelper.getInstance(this).showWarn(respond.message);
            dialog.refreshCaptcha();
        }
    }

    @Override
    public void checkCaptchaRes(HttpRespond respond) {
        if (respond.result == 1) {
            if (mType == TYPE_WECHAT) {
                mPresenter.WeChatLogin(mUid, mName, mHeadUrl, edt_phone.getText().toString());
            } else if (mType == TYPE_ALI) {
                mPresenter.ALiLogin(mUser_id, mAuth_code, edt_phone.getText().toString());
            }
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showRequestFailed(String message) {
        ToastHelper.getInstance(this).showWarn(message);
    }

    @Override
    public void showThirdLoginRes(String data) {
        if (data.equals("1")) {
            UserManager.getInstance().saveIv(mPresenter.getIv());
            UserManager.getInstance().saveKey(mPresenter.getKey());
            UserManager.getInstance().saveLoginData(edt_phone.getText().toString(), mPresenter.getLocalToken());

            ToastHelper.getInstance(this).showSucceed("登录成功");
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
    }
}
